<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('organization.audit.title') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="3">
                  <el-image class="logo" v-if="dataForm.logo" :src="dataForm.logo" :lazy="true"></el-image>
                </el-col>
                <el-col :span="21">
                  <el-descriptions border>
                    <el-descriptions-item width="200" :label="i18n('organization.chinese_title')" label-align="right" :span="3">
                      {{ dataForm.chinese_title }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.english_title')" label-align="right" :span="3">
                      {{ dataForm.english_title }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.shipowner_id')" label-align="right" :span="3">
                      {{ dataForm.shipowner_name }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.register_limit')" label-align="right" :span="3">
                      {{ dataForm.register_limit }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.valid_time')" label-align="right" :span="3">
                      {{ dataForm.valid_time }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.create_time')" label-align="right" :span="3">
                      {{ dataForm.create_time }}
                    </el-descriptions-item>
                  </el-descriptions>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.detail_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-descriptions border>
                <el-descriptions-item width="200" :label="i18n('organization.archive.chinese_description')" label-align="right" :span="3">
                  {{ dataForm.chinese_description }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('organization.archive.english_description')" label-align="right" :span="3">
                  {{ dataForm.english_description }}
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.audit_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-form-item :label="i18n('organization.audit.business_card')" prop="business_card">
                <el-link v-if="dataForm.business_card" :href="dataForm.business_card" target="_blank">
                  <el-image class="image" :src="dataForm.business_card" :lazy="true"></el-image>
                </el-link>
              </el-form-item>

              <el-form-item :label="i18n('common.audit_status')" prop="audit_status">
                <el-switch
                  v-model="dataForm.audit_status"
                  active-value="1"
                  :active-text="i18n('common.pass')"
                  inactive-value="2"
                  :inactive-text="i18n('common.no_pass')">
                </el-switch>
              </el-form-item>

              <el-form-item :label="i18n('organization.audit.content')" prop="content">
                <el-input type="textarea" :placeholder="i18n('common.please_input') + i18n('organization.audit.content')" v-model="dataForm.content" rows="3" show-word-limit maxlength="500"></el-input>
              </el-form-item>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:organization:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-audit-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('organization/audit')

  const dataForm = reactive({
    id: 0,
    logo: '',
    chinese_title: '',
    english_title: '',
    shipowner_name: '',
    register_limit: '',
    valid_time: '',
    create_time: '',
    chinese_description: '',
    english_description: '',
    business_card: '',
    audit_status: '1',
    content: '',
  })

  const dataRule = reactive({
    audit_status: [{
      required: true,
      message: i18n('organization.rules.audit.audit_status.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/organization/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.logo = data.data.logo
            dataForm.chinese_title = data.data.chinese_title
            dataForm.english_title = data.data.english_title
            dataForm.register_limit = data.data.register_limit
            dataForm.valid_time = data.data.valid_time
            dataForm.create_time = data.data.create_time

            if(data.data.shipowner) {
              dataForm.shipowner_name = data.data.shipowner.chinese_name
            }
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/organization/archive/data`),
              method: 'get',
              params: $http.adornParams({
                'organization_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                if(data.data) {
                  dataForm.chinese_description = data.data.chinese_description
                  dataForm.english_description = data.data.english_description
                }
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/organization/audit/data`),
              method: 'get',
              params: $http.adornParams({
                'organization_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                if(data.data) {
                  dataForm.business_card = data.data.business_card
                  dataForm.content = data.data.content
                }
              }
            })
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData(dataForm),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  onMounted(() => {
    init(formRef)
  })
</script>

<style lang="scss" scoped>
  .logo {
    width: 6vw;
    height: 6vw;
    min-width: 120px;
    min-height: 120px;
  }
</style>
